package com.ly.sy.util;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadVideoRequest;
import com.aliyun.vod.upload.resp.UploadVideoResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 前段视频播放的时候一定要两个都用上
 * 本人深深体会过这个痛苦
 *
 * **/

public class getPlayAuthorurl {
    private static final String ACCESKEYID = "LTAI4GAKBF1CTWDEf6kqN7V7";
    private static final String ACCESSKEYSECRET = "NsPrj9vxTQO1LAhAo3IiaGSXiSXZdw";

    //点播上传视频
    public static void uploadVideo(String title, String fileName, HttpServletRequest httpServletRequest){
//        String title = "6 - What If I Want to Move Faster-upload by sdk";
//        String fileName = "C:/Users/Administrator/Desktop/6 - What If I Want to Move Faster.mp4";
        //视频上传
        UploadVideoRequest request = new UploadVideoRequest(ACCESKEYID, ACCESSKEYSECRET, title, fileName);
        /* 可指定分片上传时每个分片的大小，默认为1M字节 */
        request.setPartSize(1 * 1024 * 1024L);
        /* 可指定分片上传时的并发线程数，默认为1，(注：该配置会占用服务器CPU资源，需根据服务器情况指定）*/
        request.setTaskNum(1);

        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadVideoResponse response = uploader.uploadVideo(request);

        if (response.isSuccess()) {
            System.out.print("VideoId=" + response.getVideoId() + "\n");
        } else {
            /* 如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因 */
            System.out.print("VideoId=" + response.getVideoId() + "\n");
            System.out.print("ErrorCode=" + response.getCode() + "\n");
            System.out.print("ErrorMessage=" + response.getMessage() + "\n");
        }

    }

    //获取视频播放凭证
    public static String getPlayAuthor(String videoId) throws ClientException {
        DefaultAcsClient client = aliyunAcsClient.initVodClient(ACCESKEYID, ACCESSKEYSECRET);
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();

//0e409bfe623a46a3bcca42af597daf84
        request.setVideoId(videoId);
        response = client.getAcsResponse(request);

        System.out.print("视频播放凭证 = " + response.getPlayAuth() + "\n");
        return response.getPlayAuth();
    }
    //获取视频的播放地址
    public static String getPlayUrl(String videoId) throws ClientException {
        DefaultAcsClient client = aliyunAcsClient.initVodClient(ACCESKEYID, ACCESSKEYSECRET);
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        GetPlayInfoResponse response = new GetPlayInfoResponse();

//0e409bfe623a46a3bcca42af597daf84
        request.setVideoId(videoId);
        response = client.getAcsResponse(request);
        List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
        //播放地址
        String videourl = null;
        for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
            System.out.print("视频播放地址 = " + playInfo.getPlayURL() + "\n");
            videourl = playInfo.getPlayURL();
        }
        //Base信息
        System.out.print("视频标题 = " + response.getVideoBase().getTitle() + "\n");
        return videourl;
    }
//可以实现批量删除视频
    public static void deteleVideo(List<String> videoId,String videourl) {

        try {
            DefaultAcsClient client = aliyunAcsClient.initVodClient(ACCESKEYID, ACCESSKEYSECRET);
            DeleteVideoResponse response = new DeleteVideoResponse();
            DeleteVideoRequest request = new DeleteVideoRequest();
            System.out.println("=================================阿里数据操作开始===================================");
            if (videourl==null){
                for(String vurl : videoId){
                    request.setVideoIds(vurl);
                }
            }else {
                request.setVideoIds(videourl);
            }
            response = client.getAcsResponse(request);
            System.out.println("===================================阿里数据操作开始=========================================");
            System.out.print("请求ID = " + response.getRequestId() + "\n");
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
        }


    }
}
